/**
 * ...
 * @author Franco Ponticelli
 */

package uform.model.core;
import uform.model.core.IValueHandler;

class Crawler
{
	public var path : String;
	public var target : Dynamic;
	public var segments : Array<String>;
	public var current : String;
	public var handler : IValueHandler<Dynamic>;
	
	public function new(path : String, target : Dynamic)
	{
		if (null == path)
			throw "null argument path";
		this.path = path;
		this.target = target;
		this.segments = path.split('.');
		this.current = null;
	}
	
	public function hasNext()
	{
		return segments.length > 0;
	}
	
	public function next()
	{
		var seg = segments.shift();
		if (null == current)
			current = seg;
		else
			current += '.' + seg;
		return seg;
	}
	
	public dynamic function get() { return target; }
	public dynamic function set(value : Dynamic) : Dynamic;
	
	public function toString()
	{
		return "[Crawler(path:" + path + "; target:" + Std.string(target) + "; segments:" + segments + "; current:" + current + ")]";
	}
}